<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java面向对象编程核心概念</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background-color: #f8fafc;
            color: #1e293b;
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
        }
        .code-block {
            background-color: #1e293b;
            color: #e2e8f0;
            border-radius: 0.5rem;
            position: relative;
        }
        .copy-btn {
            position: absolute;
            right: 0.5rem;
            top: 0.5rem;
            opacity: 0;
            transition: opacity 0.2s ease-in-out;
        }
        .code-block:hover .copy-btn {
            opacity: 1;
        }
        .concept-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .footer-link:hover {
            color: #a5b4fc;
        }
    </style>
</head>
<body class="antialiased">
    <!-- Hero Section -->
    <header class="hero-gradient text-white py-20 md:py-32 px-6">
        <div class="max-w-5xl mx-auto text-center">
            <h1 class="text-4xl md:text-6xl font-bold mb-6">Java面向对象编程核心概念</h1>
            <p class="text-xl md:text-2xl font-light max-w-3xl mx-auto leading-relaxed">
                深入理解抽象类、接口和对象转型，掌握Java多态性的精髓
            </p>
            <div class="mt-10 flex justify-center space-x-4">
                <a href="#abstract-class" class="px-6 py-3 bg-white text-indigo-600 font-medium rounded-lg shadow-md hover:bg-indigo-50 transition duration-300">
                    <i class="fas fa-shapes mr-2"></i>抽象类
                </a>
                <a href="#interface" class="px-6 py-3 bg-indigo-800 text-white font-medium rounded-lg shadow-md hover:bg-indigo-700 transition duration-300">
                    <i class="fas fa-plug mr-2"></i>接口
                </a>
                <a href="#object-casting" class="px-6 py-3 bg-indigo-800 text-white font-medium rounded-lg shadow-md hover:bg-indigo-700 transition duration-300">
                    <i class="fas fa-exchange-alt mr-2"></i>对象转型
                </a>
            </div>
        </div>
    </header>

    <!-- Main Content -->
    <main class="max-w-5xl mx-auto px-6 py-12">
        <!-- Abstract Class Section -->
        <section id="abstract-class" class="mb-20">
            <div class="flex items-center mb-8">
                <div class="bg-indigo-100 p-3 rounded-full mr-4">
                    <i class="fas fa-shapes text-indigo-600 text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold text-gray-800">抽象类</h2>
            </div>
            
            <div class="grid grid-cols-1 lg:grid-cols-3 gap-8">
                <div class="bg-white rounded-xl shadow-md overflow-hidden concept-card transition duration-300">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="bg-indigo-100 p-2 rounded-full mr-3">
                                <i class="fas fa-code text-indigo-600"></i>
                            </div>
                            <h3 class="text-xl font-semibold">定义与语法</h3>
                        </div>
                        <p class="text-gray-600 mb-4">抽象类是用<code class="bg-gray-100 px-1 py-0.5 rounded">abstract</code>关键字修饰的类，它可以包含抽象方法和非抽象方法。</p>
                        <div class="code-block p-4 rounded-lg mb-4">
                            <button class="copy-btn text-indigo-300 hover:text-white">
                                <i class="far fa-copy"></i>
                            </button>
                            <pre><code class="language-java">public abstract class AbstractClass {
    // 抽象方法
    public abstract void abstractMethod();
    
    // 非抽象方法
    public void concreteMethod() {
        // 方法体
    }
}</code></pre>
                        </div>
                    </div>
                </div>

                <div class="bg-white rounded-xl shadow-md overflow-hidden concept-card transition duration-300">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="bg-indigo-100 p-2 rounded-full mr-3">
                                <i class="fas fa-star text-indigo-600"></i>
                            </div>
                            <h3 class="text-xl font-semibold">特点与作用</h3>
                        </div>
                        <ul class="space-y-2 text-gray-600">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-indigo-500 mt-1 mr-2"></i>
                                <span>不能被实例化，只能被子类继承</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-indigo-500 mt-1 mr-2"></i>
                                <span>子类必须实现父类的抽象方法</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-indigo-500 mt-1 mr-2"></i>
                                <span>可以包含非抽象方法，提高代码复用</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-indigo-500 mt-1 mr-2"></i>
                                <span>定义一种抽象数据类型，封装相关行为</span>
                            </li>
                        </ul>
                    </div>
                </div>

                <div class="bg-white rounded-xl shadow-md overflow-hidden concept-card transition duration-300">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="bg-indigo-100 p-2 rounded-full mr-3">
                                <i class="fas fa-lightbulb text-indigo-600"></i>
                            </div>
                            <h3 class="text-xl font-semibold">使用场景</h3>
                        </div>
                        <p class="text-gray-600 mb-4">当多个类具有相似的属性和行为时，可以将这些共性抽象出来，定义为一个抽象类。</p>
                        <div class="code-block p-4 rounded-lg">
                            <button class="copy-btn text-indigo-300 hover:text-white">
                                <i class="far fa-copy"></i>
                            </button>
                            <pre><code class="language-java">public abstract class Animal {
    // 抽象方法
    public abstract void eat();
    
    // 非抽象方法
    public void run() {
        System.out.println("Animal is running.");
    }
}</code></pre>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Interface Section -->
        <section id="interface" class="mb-20">
            <div class="flex items-center mb-8">
                <div class="bg-purple-100 p-3 rounded-full mr-4">
                    <i class="fas fa-plug text-purple-600 text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold text-gray-800">接口</h2>
            </div>
            
            <div class="grid grid-cols-1 lg:grid-cols-3 gap-8">
                <div class="bg-white rounded-xl shadow-md overflow-hidden concept-card transition duration-300">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="bg-purple-100 p-2 rounded-full mr-3">
                                <i class="fas fa-code text-purple-600"></i>
                            </div>
                            <h3 class="text-xl font-semibold">定义与语法</h3>
                        </div>
                        <p class="text-gray-600 mb-4">接口是用<code class="bg-gray-100 px-1 py-0.5 rounded">interface</code>关键字定义的，可以包含抽象方法、默认方法和静态方法。</p>
                        <div class="code-block p-4 rounded-lg mb-4">
                            <button class="copy-btn text-purple-300 hover:text-white">
                                <i class="far fa-copy"></i>
                            </button>
                            <pre><code class="language-java">public interface Animal {
    // 抽象方法
    void eat();
    void run();
    
    // 默认方法
    default void sleep() {
        System.out.println("Animal is sleeping");
    }
}</code></pre>
                        </div>
                    </div>
                </div>

                <div class="bg-white rounded-xl shadow-md overflow-hidden concept-card transition duration-300">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="bg-purple-100 p-2 rounded-full mr-3">
                                <i class="fas fa-star text-purple-600"></i>
                            </div>
                            <h3 class="text-xl font-semibold">特点与作用</h3>
                        </div>
                        <ul class="space-y-2 text-gray-600">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-purple-500 mt-1 mr-2"></i>
                                <span>定义一组规范，要求实现类必须实现</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-purple-500 mt-1 mr-2"></i>
                                <span>支持多继承，一个类可实现多个接口</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-purple-500 mt-1 mr-2"></i>
                                <span>成员变量默认为<code class="bg-gray-100 px-1 py-0.5 rounded">public static final</code></span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-purple-500 mt-1 mr-2"></i>
                                <span>提供代码灵活性和可维护性</span>
                            </li>
                        </ul>
                    </div>
                </div>

                <div class="bg-white rounded-xl shadow-md overflow-hidden concept-card transition duration-300">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="bg-purple-100 p-2 rounded-full mr-3">
                                <i class="fas fa-random text-purple-600"></i>
                            </div>
                            <h3 class="text-xl font-semibold">与抽象类比较</h3>
                        </div>
                        <div class="overflow-x-auto">
                            <table class="min-w-full divide-y divide-gray-200">
                                <thead class="bg-gray-50">
                                    <tr>
                                        <th class="px-4 py-2 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">特性</th>
                                        <th class="px-4 py-2 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">接口</th>
                                        <th class="px-4 py-2 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">抽象类</th>
                                    </tr>
                                </thead>
                                <tbody class="bg-white divide-y divide-gray-200">
                                    <tr>
                                        <td class="px-4 py-2 whitespace-nowrap text-sm text-gray-600">方法实现</td>
                                        <td class="px-4 py-2 whitespace-nowrap text-sm text-gray-600">默认抽象</td>
                                        <td class="px-4 py-2 whitespace-nowrap text-sm text-gray-600">可含实现</td>
                                    </tr>
                                    <tr>
                                        <td class="px-4 py-2 whitespace-nowrap text-sm text-gray-600">继承</td>
                                        <td class="px-4 py-2 whitespace-nowrap text-sm text-gray-600">多继承</td>
                                        <td class="px-4 py-2 whitespace-nowrap text-sm text-gray-600">单继承</td>
                                    </tr>
                                    <tr>
                                        <td class="px-4 py-2 whitespace-nowrap text-sm text-gray-600">变量</td>
                                        <td class="px-4 py-2 whitespace-nowrap text-sm text-gray-600">只能是常量</td>
                                        <td class="px-4 py-2 whitespace-nowrap text-sm text-gray-600">普通变量</td>
                                    </tr>
                                    <tr>
                                        <td class="px-4 py-2 whitespace-nowrap text-sm text-gray-600">用途</td>
                                        <td class="px-4 py-2 whitespace-nowrap text-sm text-gray-600">定义规范</td>
                                        <td class="px-4 py-2 whitespace-nowrap text-sm text-gray-600">定义类型</td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Object Casting Section -->
        <section id="object-casting" class="mb-20">
            <div class="flex items-center mb-8">
                <div class="bg-teal-100 p-3 rounded-full mr-4">
                    <i class="fas fa-exchange-alt text-teal-600 text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold text-gray-800">对象转型</h2>
            </div>
            
            <div class="grid grid-cols-1 lg:grid-cols-2 gap-8">
                <div class="bg-white rounded-xl shadow-md overflow-hidden concept-card transition duration-300">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="bg-teal-100 p-2 rounded-full mr-3">
                                <i class="fas fa-arrow-up text-teal-600"></i>
                            </div>
                            <h3 class="text-xl font-semibold">向上转型</h3>
                        </div>
                        <p class="text-gray-600 mb-4">将子类对象转换为父类类型，是安全的隐式转换。</p>
                        <div class="code-block p-4 rounded-lg mb-4">
                            <button class="copy-btn text-teal-300 hover:text-white">
                                <i class="far fa-copy"></i>
                            </button>
                            <pre><code class="language-java">class Parent {}
class Child extends Parent {}

public class Main {
    public static void main(String[] args) {
        Child child = new Child();
        Parent parent = child; // 向上转型
    }
}</code></pre>
                        </div>
                    </div>
                </div>

                <div class="bg-white rounded-xl shadow-md overflow-hidden concept-card transition duration-300">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="bg-teal-100 p-2 rounded-full mr-3">
                                <i class="fas fa-arrow-down text-teal-600"></i>
                            </div>
                            <h3 class="text-xl font-semibold">向下转型</h3>
                        </div>
                        <p class="text-gray-600 mb-4">将父类对象转换为子类类型，需要使用<code class="bg-gray-100 px-1 py-0.5 rounded">instanceof</code>检查以避免异常。</p>
                        <div class="code-block p-4 rounded-lg">
                            <button class="copy-btn text-teal-300 hover:text-white">
                                <i class="far fa-copy"></i>
                            </button>
                            <pre><code class="language-java">class Parent {}
class Child extends Parent {}

public class Main {
    public static void main(String[] args) {
        Parent parent = new Child();
        if (parent instanceof Child) {
            Child child = (Child) parent; // 向下转型
        }
    }
}</code></pre>
                        </div>
                    </div>
                </div>
            </div>

            <div class="mt-8 bg-white rounded-xl shadow-md overflow-hidden">
                <div class="p-6">
                    <div class="flex items-center mb-4">
                        <div class="bg-teal-100 p-2 rounded-full mr-3">
                            <i class="fas fa-project-diagram text-teal-600"></i>
                        </div>
                        <h3 class="text-xl font-semibold">对象转型关系图</h3>
                    </div>
                    <div class="mermaid">
                        classDiagram
                            class Animal {
                                +eat()
                            }
                            class Dog {
                                +bark()
                            }
                            class Cat {
                                +meow()
                            }
                            Animal <|-- Dog
                            Animal <|-- Cat
                    </div>
                    <p class="text-gray-600 mt-4">向上转型和向下转型是多态性的重要实现机制，允许将对象视为其超类或子类对象。</p>
                </div>
            </div>
        </section>

        <!-- Homework Section -->
        <section id="homework" class="mb-20">
            <div class="flex items-center mb-8">
                <div class="bg-orange-100 p-3 rounded-full mr-4">
                    <i class="fas fa-tasks text-orange-600 text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold text-gray-800">作业练习</h2>
            </div>
            
            <div class="grid grid-cols-1 lg:grid-cols-2 gap-8">
                <div class="bg-white rounded-xl shadow-md overflow-hidden">
                    <div class="bg-orange-500 text-white px-6 py-4">
                        <h3 class="text-xl font-semibold">作业一</h3>
                    </div>
                    <div class="p-6">
                        <p class="text-gray-600 mb-4">
                            创建一个接口（接口中包含fun1()和fun2()两个方法）和实现这个接口的类，并在类中添加一个fun3()方法，在测试类中，使用new关键字创建一个类的对象，并把这个对象赋给接口类型的引用，使用这个引用调用fun3()，观察编译结果，并分析为什么是这个结果？
                        </p>
                        <div class="code-block p-4 rounded-lg">
                            <button class="copy-btn text-orange-300 hover:text-white">
                                <i class="far fa-copy"></i>
                            </button>
                            <pre><code class="language-java">// 提示：思考接口引用的编译时类型和运行时类型</code></pre>
                        </div>
                    </div>
                </div>

                <div class="bg-white rounded-xl shadow-md overflow-hidden">
                    <div class="bg-orange-500 text-white px-6 py-4">
                        <h3 class="text-xl font-semibold">作业二</h3>
                    </div>
                    <div class="p-6">
                        <p class="text-gray-600 mb-4">
                            根据生活案例，构思一个采用接口或抽象类的多态应用案例，或者两者结合更好。要求先用文字描述下该场景及相关对象之间的关系，然后用代码来描述实现该场景。
                        </p>
                        <div class="code-block p-4 rounded-lg">
                            <button class="copy-btn text-orange-300 hover:text-white">
                                <i class="far fa-copy"></i>
                            </button>
                            <pre><code class="language-java">// 示例场景：支付系统
// 可以定义一个Payment抽象类或Payment接口
// 然后实现不同的支付方式：Alipay、WechatPay、BankCardPay等</code></pre>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Concept Map -->
        <section class="mb-20">
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="p-6">
                    <div class="flex items-center mb-4">
                        <div class="bg-indigo-100 p-2 rounded-full mr-3">
                            <i class="fas fa-brain text-indigo-600"></i>
                        </div>
                        <h3 class="text-xl font-semibold">面向对象核心概念关系图</h3>
                    </div>
                    <div class="mermaid">
                        mindmap
                            root((面向对象))
                                封装
                                    数据隐藏
                                    访问控制
                                继承
                                    单继承
                                    多继承(通过接口)
                                多态
                                    方法重写
                                    对象转型
                                        向上转型
                                        向下转型
                                    抽象类与接口
                                抽象类
                                    含抽象方法
                                    可含实现
                                    单继承
                                接口
                                    纯抽象(Java 8前)
                                    默认方法
                                    多继承
                    </div>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-8">
        <div class="max-w-5xl mx-auto px-6">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-lg font-semibold text-white">技术小馆</h3>
                    <p class="text-gray-400">探索编程之美，领悟技术之道</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="footer-link hover:text-white transition duration-300">
                        <i class="fas fa-external-link-alt mr-2"></i>访问技术小馆
                    </a>
                </div>
            </div>
            <div class="mt-8 pt-8 border-t border-gray-800 text-center text-sm text-gray-500">
                &copy; 2023 技术小馆. All rights reserved.
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'dark',
            flowchart: { useMaxWidth: true, htmlLabels: true },
            securityLevel: 'loose'
        });

        // Copy button functionality
        document.querySelectorAll('.copy-btn').forEach(button => {
            button.addEventListener('click', () => {
                const codeBlock = button.parentElement.querySelector('code');
                const range = document.createRange();
                range.selectNode(codeBlock);
                window.getSelection().removeAllRanges();
                window.getSelection().addRange(range);
                document.execCommand('copy');
                window.getSelection().removeAllRanges();
                
                const originalIcon = button.innerHTML;
                button.innerHTML = '<i class="fas fa-check"></i>';
                button.classList.add('text-green-400');
                
                setTimeout(() => {
                    button.innerHTML = originalIcon;
                    button.classList.remove('text-green-400');
                }, 2000);
            });
        });
    </script>
</body>
</html>